package com.umfwechat.h5.controller.settlement;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.umfwechat.common.ReqMessage;
import com.umfwechat.common.code.RetCode;
import com.umfwechat.common.constant.CommonConsts;
import com.umfwechat.common.constant.ConfigConsts;
import com.umfwechat.common.constant.MsgConsts;
import com.umfwechat.common.constant.ValueConsts;
import com.umfwechat.common.prop.UmfPropModel;
import com.umfwechat.util.HttpUtil;
import com.umfwechat.util.ReqMessageUtil;
import com.umfwechat.util.ResultJsonUtil;
import com.umfwechat.util.StringUtil;

/**
 * e秒付付款
 * @author css
 *
 */
@Controller
@RequestMapping("h5/epayController")
public class EpayController {

	private static Logger logger = LoggerFactory.getLogger(EpayController.class);
	
	/**
	 * e秒付付款接口
	 * @return
	 * merId
	 */
	@RequestMapping(value="/epay",method = {RequestMethod.POST,RequestMethod.GET },produces="text/html;charset=UTF-8")
	@ResponseBody
	public String epay(HttpServletRequest request , HttpServletResponse response ,String merId , String amount ,String bankAccount,
			String colAccName, String orderId){
		logger.info("【e秒付付款接口】开始");
		try {
			if(StringUtil.isEmpty(orderId)){
				logger.info("获取订单号为空");
				return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433077);
			}
			if(StringUtil.isEmpty(merId)){
				logger.info("获取商户号为空");
				return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433077);
			}
			if(StringUtil.isEmpty(bankAccount)){
				logger.info("获取银行卡号为空");
				return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433077);
			}
			if(StringUtil.isEmpty(amount)){
				logger.info("获取申请提前结算金额为空");
				return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433077);
			}
			if(StringUtil.isEmpty(colAccName)){
				logger.info("获取银行卡号账户名为空");
				return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433077);
			}
			//金额元转分
			BigDecimal bigAmount = new BigDecimal(amount);
			BigDecimal num = new BigDecimal(100);
			amount = bigAmount.multiply(num).setScale(0).toString();
			//~~~~~~~~~~~~~~~~~~~~~~~~~~e秒付款接口~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
			String url = UmfPropModel.INSTANCE.getPropValue(ConfigConsts.EPAY_URL);
			Map<String , Object> param = new HashMap<String, Object>();
			ReqMessage req = ReqMessageUtil.makeReq();
			param.put(CommonConsts.RPID, req.getRpid());
			param.put(CommonConsts.REQDATE,req.getReqDate());
			param.put(CommonConsts.REQTIME,req.getReqTime());
			param.put(CommonConsts.MERID, merId);
			param.put(CommonConsts.ORDERID, orderId);
			SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
			String orderDate = df.format(new Date());
			logger.info("当前时间为：" + orderDate );
			param.put(CommonConsts.ORDERDATE, orderDate);
			param.put(CommonConsts.ORIGAMT, amount);
			param.put(CommonConsts.INACCOUNTPRO, ValueConsts.ZERO);
			param.put(CommonConsts.INMEDIAID, bankAccount);
			param.put(CommonConsts.INACCOUNTNAME, colAccName);
			param.put(CommonConsts.INBANKACCTYPE, ValueConsts.INBANKACCTYPE);
			param.put(CommonConsts.CUTFEETYPE, ValueConsts.STATE_TWO);
//			param.put(CommonConsts.PRODUCTID,ValueConsts.PRODUCTID_ES_PAYMENT_CODE);//不传值根据INACCOUNTPRO
			logger.info("【e秒付付款接口】入参" + param);
			logger.info("请求地址" + url);
			Map<String, Object> resMap = HttpUtil.httpPostForm2Xml(url, param);
			logger.info("【e秒付付款接口】出参" + resMap);
			String state = CommonConsts.SPACE ;
			Map<String , Object> resultMap = new HashMap<String ,Object>();
			//retcode不等于0000时
			if(!resMap.get(CommonConsts.RETCODE).equals(RetCode.SUCCESS)){
				logger.info("retcode：" + resMap.get(CommonConsts.RETCODE));
				//不明
				if(resMap.get(CommonConsts.RETCODE).equals("00072046") || resMap.get(CommonConsts.RETCODE).equals("00180008") ||
					resMap.get(CommonConsts.RETCODE).equals("00180021") || resMap.get(CommonConsts.RETCODE).equals("00130001") ||
					resMap.get(CommonConsts.RETCODE).equals("00130002")){
					logger.info("e秒付交易不明。。。");
					//标记交易不明
					state = ValueConsts.TYPE_ONE;
					resultMap.put(CommonConsts.STATE, state);
					return ResultJsonUtil.resultJson(RetCode.SUCCESS, MsgConsts.E00433116 ,resultMap); 
				}
				if(resMap.get(CommonConsts.RETCODE).equals("00131012")){
					logger.info("e秒付交易不明。。。");
					state = ValueConsts.TYPE_TWO;
					resultMap.put(CommonConsts.STATE, state);
					return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433118 ,resultMap); 

				}
				logger.info("e秒付交易失败。。。");
				//标记交易失败
				state = ValueConsts.TYPE_TWO;
				resultMap.put(CommonConsts.STATE, state);
				return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433117 ,resultMap);
			}	
			//retcode=0000判断orderState
			String orderState = null ; 
			orderState = resMap.get(CommonConsts.ORDERSTATE).toString();
			logger.info("订单状态orderState：" + orderState);
			if(orderState.equals(ValueConsts.APP_CHANNEL_STATE_FOUR)){
				logger.info("e秒付交易成功。。。");
				//标记交易成功
				state = ValueConsts.ZERO;
				resultMap.put(CommonConsts.STATE, state);
				return ResultJsonUtil.resultJson(RetCode.SUCCESS,MsgConsts.I00433112 ,resultMap);
			} 
			if(orderState.equals(ValueConsts.TYPE_ONE) || orderState.equals(ValueConsts.SERVTYPE) || orderState.equals(ValueConsts.SIXTEEN)){
				logger.info("e秒付交易不明。。。");
				//标记交易不明
				state = ValueConsts.TYPE_ONE;
				resultMap.put(CommonConsts.STATE, state);
				return ResultJsonUtil.resultJson(RetCode.SUCCESS, MsgConsts.E00433116 ,resultMap);
			} 
			if(orderState.equals(ValueConsts.STATE_THREE)){
				logger.info("e秒付交易失败。。。");
				//标记交易失败
				state = ValueConsts.TYPE_TWO;
				resultMap.put(CommonConsts.STATE, state);
				return ResultJsonUtil.resultJson(RetCode.SUCCESS, MsgConsts.E00433117 ,resultMap);
			} 
		} catch (Exception e) {
			logger.info("e秒付付款失败!");
		}
		return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433117 );
	}
}
